package com.atguigu.app;

import com.atguigu.bean.Bean1;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.kafka.common.protocol.types.Field;

//watermark在不同位置下 多并行度下watermark传递的问题
public class WindowTest02 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        //此时
        SingleOutputStreamOperator<Bean1> bean1DS = env.socketTextStream("hadoop102", 8888)
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<String>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<String>() {
                                    @Override
                                    public long extractTimestamp(String element, long recordTimestamp) {
                                        String[] split = element.split(",");
                                        return Long.parseLong(split[2])*1000;
                                    }
                                })
                )
                .map(new MapFunction<String, Bean1>() {
                    @Override
                    public Bean1 map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new Bean1(split[0], split[1], Long.parseLong(split[2]));
                    }
                });


        bean1DS.keyBy(s->s.getTs())
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .max("name")//求最大值，但是我们传1 2 3 4 5这些
                .print();

        env.execute();
    }
}
